#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayIpRouting < Aio::Module::Cmd::H3C
	include Aio::Module

	def initialize
		super({
			:cmd_full		=> "display ip routing-table",
			:cmd_short	=> "dis ip rout",
			:author			=> "Elin",
			:ranking		=> Ranking_2,
			:description	=> "This is H3C Command# display ip routing-table",
			:platform		=> "all",
			:benchmark	=> {}
		})
	end

	def parse
		cont = self.context.dup
		routing = {}
		useful[:routing] = routing
		
		cont.readline_match_block(/Destinations : (?<destinations>\d+) Routes : (?<routes>\d+)/) do |block|
			block.update(routing, :destinations)
			block.update(routing, :routes)
		end
		cont.readline_match_block(//)
		cont.readline_match_block(/Destination\/Mask Proto Pre Cost NextHop Interface/)
		cont.readline_match_block(//)
		loop do 
			cont.readline_range_if(/^\d+/, /^\s+/) do |cont_layer|
				parse_routing(cont_layer)
			end
		end
	end

	def parse_routing(context)
		router = nil
		context.readline_match_block(/(?<dest>[^\s]*) (?<proto>[^\s]*) (?<pre>\d+) (?<cost>\d+) (?<next_hop>.*) (?<iface>.*)/) do |block|
			route = {}
			router = block[:dest]
			useful[:routing][router] = {}
			useful[:routing][router][:first] = route
			block.update(useful[:routing][router], :dest)
			block.update(route, :proto)	
			block.update(route, :pre)
			block.update(route, :cost)
			block.update(route, :next_hop)
			block.update(route, :iface)
		end

		if context.size == 2
			context.readline_match_block(/ (?<proto>.*) (?<pre>\d+) (?<cost>\d+) (?<next_hop>.*) (?<iface>.*)/) do |block|
				route = {}
				router = block[:dest]
				useful[:routing][router][:secend] = route
				block.update(route, :proto)
				block.update(route, :pre)
				block.update(route, :cost)
				block.update(route, :next_hop)
				block.update(route, :iface)
			end
		end
	end
end

